<template>
  <div class="edit" v-if="editShow">
    <div class="first">{{editType}}</div>
 
<van-divider />
       <van-field
  v-model="form.DATE"
  is-link
  readonly
  label="预定日期"
  placeholder="请选择选项"
  @click="showTime = true"
/>
 <van-popup v-model="showTime" round position="bottom" :style="{ height: '40%' }" >
      <van-datetime-picker
  type="date"
  title="选择年月日"
   @cancel="showTime = false"
   @confirm="confirm1"
  :min-date="minDate"
  :max-date="maxDate"
/>
    </van-popup>
 

   <van-field
  v-model="form.fieldType"
  is-link
  readonly
  label="套餐类型"
  placeholder="请选择选项"
  @click="showMeal = true"
/>
 <van-popup v-model="showMeal" round position="bottom" :style="{ height: '40%' }" >
      <van-area title="套餐类型" @cancel="showMeal = false" @confirm="confirm2" columns-num="1" :area-list="mealList" value />
    </van-popup>

     
    <van-field
  v-model="form.PLACE"
  is-link
  readonly
  label="就餐地点"
  placeholder="请选择选项"
  @click="showAddress = true"
/>
 <van-popup v-model="showAddress" round position="bottom" :style="{ height: '40%' }" >
      <van-area title="请选择就餐地点" @cancel="showAddress = false" @confirm="confirm3" columns-num="1" :area-list="areaList" value />
    </van-popup>
 
    <div class="wwd">
      <p >套餐份数</p>
      <input v-model="form.ORDERCOUNT" label="套餐份数" placeholder="输入数字" type="number" />
    </div>

    <div class="twoBtn">
      <van-button @click="closeFn">取消</van-button>
    <van-button type="info" @click="handleDeter">确定</van-button>
    </div>
  </div>

</template>

<script>
import { Toast } from 'vant';
export default {

  props:{
    skip:{
      type:Number
    },
    editShow:{
      type:Boolean
    },
    editMessage:{
      type:Object
    },
    editType:{
      type:String
    },
    referenceList:{
      type:Array
    },
    datalist:{
      type:Array,
      default:()=>[]
    }
  },
  updated(){
    
    this.form = this.editMessage
  },
  
 data() {
    return {
      form:{
        "id":new Date().getTime(),
        "DATE":'',//预定日期
        "fieldType":'',//套餐类型
        "TYPE":'',  //套餐类型对应的数字
        "PLACE":'',//请选择就餐地点
        "TOTALMONEY":'',//总价
        "ORDERCOUNT":``,//套餐份数
        "MONEY":``,//单价
        "DESCRIBEW":'null',
        "EM": 'null', 
        "QCZT": '1',//取餐状态
        "checked":false
      },
       minDate: new Date(new Date(new Date().getTime()+86400000).getFullYear(), new Date(new Date().getTime()+86400000).getMonth(), new Date(new Date().getTime()+86400000).getDate()),
       maxDate: new Date(new Date(new Date().getTime()+86400000*7).getFullYear(), new Date(new Date().getTime()+86400000*7).getMonth(), new Date(new Date().getTime()+86400000*7).getDate()),
       
      currentDate: new Date(),
      date: '',
      showTime:false,
      showAddress:false,
      showMeal: false,
       mealList:{
        province_list:{
          1: '早餐',
          2: '午餐',
          3: '晚餐',
        },
      },
       areaList:{
        province_list:{
          1: '农创园',
          2: '清华力合',
          3: '永宁',
        },
      },
    };
  },
  mounted(){
    
  },
  methods:{
    handleDeter(){
      
      
      let MONEY = 1
      let TYPE = 0
      if(this.form.fieldType=="早餐"){ TYPE = "1", MONEY= 5}
      if(this.form.fieldType=="午餐"){ TYPE = "2" ,MONEY = 20}
      if(this.form.fieldType=="晚餐"){ TYPE = "3", MONEY = 20}
      const TOTALMONEY = MONEY*this.form.ORDERCOUNT //总价
      this.form.TOTALMONEY = TOTALMONEY
      this.form.MONEY = MONEY
      this.form.TYPE = TYPE
        if(this.form.DATE===""){
          Toast.fail('请填写完整')
          return
        }
        if(this.form.fieldType===""){
          Toast.fail('请填写完整')
          return
        }
        if(this.form.ORDERCOUNT===""){
          Toast.fail('请填写完整')
          return
        }
        if(this.form.ORDERCOUNT<=0){
          Toast.fail('套餐份数有误')
          return
        }
        if(this.form.PLACE===""){
          Toast.fail('请填写完整')
          return
        }
      try{
        if(this.datalist!==[]&&this.editType=="新增预定"){
         
        this.datalist.forEach(item => {
          if(item.DATE==this.form.DATE&&item.fieldType==this.form.fieldType&&item.PLACE==this.form.PLACE){
            Toast.fail('已新建')
            throw("已新建")
          }
        });
      }
      }catch{
        return 
      }
      
      if(this.skip==1){
        try{
        this.referenceList.forEach(item => {

        if(item.DATE.replace(/[^\d]/g, "") == this.form.DATE.replace(/[^\d]/g, "")){
          let typeBr = item.ISBR.split(";")
          let typeLu = item.ISLU.split(";")
          let typeSu = item.ISSU.split(";")
          console.log("type",typeBr);
          if(this.form.fieldType=='早餐'&&(item.isBreakfast!=1||item.ISBR!=0)&&typeBr.includes(this.form.PLACE)){Toast.fail('该日期下已存在该套餐类型');throw(item.BREAKFAST)}else{this.form.DESCRIBEW = item.BREAKFAST}
          if(this.form.fieldType=='午餐'&&(item.isDinner!=1||item.ISLU!=0)&&typeLu.includes(this.form.PLACE)){Toast.fail('该日期下已存在该套餐类型');throw(item.LUNCH) }else{this.form.DESCRIBEW = item.LUNCH}
          if(this.form.fieldType=='晚餐'&&(item.isLunch!=1||item.ISSU!=0)&&typeSu.includes(this.form.PLACE)){Toast.fail('该日期下已存在该套餐类型');throw(item.SUPPER)}else{this.form.DESCRIBEW = item.SUPPER}
        }
        if (item.DATE.replace(/[^\d]/g, "") == this.form.DATE.replace(/[^\d]/g, "")) {
          if(this.form.fieldType=='早餐'){this.form.DESCRIBEW = item.BREAKFAST}
          if(this.form.fieldType=='午餐'){this.form.DESCRIBEW = item.LUNCH}
          if(this.form.fieldType=='晚餐'){this.form.DESCRIBEW = item.SUPPER}
        }
        
      });

      }catch(e){
        console.log(e);
        return 
      }
      }
     
      this.$emit("updata:data",this.form)
       this.$emit("updata:node",this.form)
      this.$emit("updata:state",false)
     
    },
    closeFn(){
      this.$emit("updata:state",false)
    },
     formatDate(date) {
      return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    confirm1(database){
      function checkTime(i){
          if(i<10){
          i='0'+i
          }
          return i
          }
        // var time='Tue Feb 26 2019 00:00:00 GMT+0800 (中国标准时间)'
        var date;
        date= new Date(database)
        var dateTime = date.getFullYear()+'-'+checkTime(date.getMonth()+1)+'-'+checkTime(date.getDate());
        this.form.DATE = dateTime
        this.showTime = false
        },
    confirm2(database){
      this.form.fieldType = database[0].name
      this.showMeal = false
    },
    confirm3(database){
      this.form.PLACE = database[0].name
      this.showAddress = false
    },
  }
}
</script>

<style scoped>
div >>> .van-cell{
  line-height: 38px;
}
.wwd{
  display: flex;
  padding-left: 17px;
  margin-bottom: 10px;
}
.wwd p{
  width: 6.2em;
}
.wwd input{
  margin-left: 10px;
  outline: none;
  border: none;
  padding-left:10px;
}
.twoBtn{
  width: 100%;
  display: flex;
  justify-content:space-around;
  color:"#1989fa";
}
.edit{
    width:300px;
    height: 350px;
    position: fixed;
    background:white;
    border: 1px solid rgb( 153, 153,153);
    border-radius: 8px;
    /* box-shadow: 2px 2px 2px 2px black; */
    top:0;
    left:0;
    bottom: 0;
    right: 0;
    margin: auto;
}
.first{
    text-align:center ;
    margin: 10px 0px ;
}
</style>